﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mail;

namespace Misc
{
    public class Mailer
    {
        public Mailer()
        {
            _from = "plic.jv.2013@gmail.com";
            _password = "superkikoo";
        }

        public bool send(string destination, string subject, string body)
        {
            try
            {
                MailFormat pFormat = MailFormat.Html;
                string pAttachmentPath = string.Empty;

                System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                mail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                                  "smtp.gmail.com");
                mail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                                  "465");
                mail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                                  "2");
                mail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                    _from);
                mail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                     _password);
                mail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                     "true");
                mail.From = _from;
                mail.To = destination;
                mail.Subject = subject;
                mail.BodyFormat = pFormat;
                mail.Body = body;
                if (pAttachmentPath.Trim() != "")
                {
                    MailAttachment MyAttachment =
                            new MailAttachment(pAttachmentPath);
                    mail.Attachments.Add(MyAttachment);
                    mail.Priority = System.Web.Mail.MailPriority.High;
                }

                SmtpMail.SmtpServer = "smtp.gmail.com:465";
                SmtpMail.Send(mail);
            }
            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }

        private string _from;
        private string _password;
    }
}
